package pkg.chemdrm;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.HorizontalAlign;

/* The Inventory class is a giant sprite that has a bunch of "children" sprites attached to it. */
public class Inventory extends Sprite  {
	private InventoryHandler mInventoryHandler;
	private Text mFinalizeText, mSetActiveText;
	
	public Inventory(ResourceHandler pResourceHandler, InventoryHandler pInventoryHandler, float pX, float pY, float pWidth, float pHeight, TextureRegion pTextureRegion) 
	{
		super(pX, pY, pWidth, pHeight, pTextureRegion);
		// for now the textureRegion will just be a black-ish kind of screen thing... we can ask Jerome to get us an image later.
		
		mInventoryHandler = pInventoryHandler;
		//mFinalizeText = new Text(30, 230, pResourceHandler.getFont("Inventory"), "Finalize Ingredients", HorizontalAlign.CENTER);
		//mSetActiveText = new Text(280, 230, pResourceHandler.getFont("Inventory"), "Set Active", HorizontalAlign.CENTER);
		
		mFinalizeText = new Text(30, 230, pResourceHandler.getFont("Inventory"), "Finalize Ingredients", HorizontalAlign.CENTER);
		mSetActiveText = new Text(280, 230, pResourceHandler.getFont("Inventory"), "Set Active", HorizontalAlign.CENTER);
		
		attachChild(mFinalizeText);
		attachChild(mSetActiveText);
		
	}
	
	public void showInventory()
	{
		setVisible(true); // tell AndEngine to draw me
		
	//	mFinalizeText.detachSelf();
		
	//	attachChild(mFinalizeText);
		
	//	mSetActiveText.detachSelf();
	//	attachChild(mSetActiveText);
		
		for(InventoryObject e : mInventoryHandler.getInventoryObjects())
		{
			e.detachSelf();

			attachChild(e);
		}
		
		
	}
	
	public void hideInventory()
	{
		setVisible(false); // don't draw me bro.
		
//		this.detachChildren();
	}
	
	public boolean isInventoryOpen()
	{
		return this.isVisible();
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
	{
		float x = pTouchAreaLocalX + 50;
		float y = pTouchAreaLocalY + 25;
		
		if(!this.isVisible())
			return false;
		
		System.err.println("Touched in Inventory " + " X: " + pTouchAreaLocalX + " Y: " + pTouchAreaLocalY);
		for(InventoryObject e : mInventoryHandler.getInventoryObjects())
		{
			if(e.contains(x, y) && pSceneTouchEvent.isActionUp())
			{
				e.touch();
			}
		}
			
		
		// iterate through the elements that are selected	
		if(mFinalizeText.contains(x, y))
		{
			System.err.println("FINALIZING!!!!one11");
			for(InventoryObject e : mInventoryHandler.getInventoryObjects())
			{
				// add them to the mixing list
				
				if(e.mIsSelected)
					mInventoryHandler.addSubstanceToMixingList(e.mSubstance);
				
			}
			// afterwards we finalize
			mInventoryHandler.finalizeMixingList();
			
			// untouch all of the items that are selected.
			for(InventoryObject e : mInventoryHandler.getInventoryObjects())
			{
				// add them to the mixing list
				if(e.mIsSelected)
					e.touch();
				
			}
			
		}
	
		if(mSetActiveText.contains(x,y))
		{
			System.err.println("SETTING ACTIVE SUBSTANCE");
			for(InventoryObject e : mInventoryHandler.getInventoryObjects())
			{
				if(e.mIsSelected)
				{
					mInventoryHandler.setActiveSubstance(e.mSubstance);
					hideInventory();
					break;
				}
			}
		}
		
		
		
		return true;
		
	}
	
}
